//
//  LAB_SaveViewController.m
//  Camera
//
//  Created by Bodi on 16/8/16.
//  Copyright © 2016年 葛海一. All rights reserved.
//

#import "LAB_SaveViewController.h"

@interface LAB_SaveViewController ()

@end

@implementation LAB_SaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavViewUI];
    [self.view addSubview:self.doneImageView];
    [self saveBtnui];
}
- (void)setNavViewUI{
    self.navView.backgroundColor = [UIColor whiteColor];
    [self addNavViewTitle:@"编辑完成"];
    self.navTitle.textColor = [UIColor blackColor];
    [self addBackButton];


}
#pragma mark - 编辑完成的图片
- (UIImageView *)doneImageView{
    if (!_doneImageView) {
        _doneImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 50, WIDTH, WIDTH)];
        
        
//        CGSize finalSize = [self.doneImageView.image size];
//        CGSize hatSize = [self.image size];
//        UIGraphicsBeginImageContext(finalSize);
//        [self.doneImageView.image drawInRect:CGRectMake(0,0,finalSize.width,finalSize.height)];
//        [self.image drawInRect:CGRectMake(0,0,hatSize.width,hatSize.height)];
//        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
//        
//        _doneImageView.image = newImage;
//        NSLog(@"aaaaa%@", newImage);

        
        
    }
    return _doneImageView;
}
#pragma mark - 保存图片按钮
- (void)saveBtnui{
            _saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH/ 2- 100, HEIGHT *0.8, 200, 50)];
        [_saveBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
        _saveBtn.backgroundColor = [UIColor redColor];
        _saveBtn.layer.cornerRadius = 10;
        _saveBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_saveBtn];
    
}
- (void)save:(UIButton *)button{
    UIImageWriteToSavedPhotosAlbum(self.doneImageView.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
    
   
    
}
- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
    
    if(!error) {
        
//        NSLog(@"成功图片保存到相册");
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:action2];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];

        
    }else{
        
//        NSLog(@"%@",error.localizedDescription);
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"保存失败" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:action2];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];

        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
